<template>
  <div class="home">
    <div class="itemTag" v-for="(item, index) in dataList" :key="index">
      <img @click="scale(item)" :src="item.url" alt v-ClickAnimation />
      <p>{{item.name}}</p>
    </div>
  </div>
  <div class="appWindow" ref="appWindow" v-show="edge">
    <div class="titleBar">
      <div class="titleBarLeft">
        <div>
          <span>New Tab</span>
          <i class="el-icon-close" @click="closeWindow('edge')"></i>  
        </div>
      </div>
      <NavBarRight close='edge' />
    </div>
    <div class="draggBox">
      <div class="urlLeft">
        <i class="el-icon-back" @click="urlLeft('back')"></i>
        <i class="el-icon-right" @click="urlLeft('go')"></i>
        <i class="el-icon-refresh-right" @click="urlLeft('refresh')"></i>
      </div>
      <el-input v-model="edgeLink" prefix-icon="el-icon-lock" size='medium' autofocus @change='edgeLinkChange'></el-input>
    </div>
    <div class="box2">
      <iframe :src="edgeLinkData"></iframe>
    </div>
  </div>
</template>

<script>
import { computed, reactive, ref, watch } from 'vue';
import { useStore } from 'vuex';
import Widget from '@/components/Widget'
import Notice from '@/components/Notice'
import NavBarRight from '@/components/NavBarRight'
import {useRouter} from 'vue-router'
export default {
  name: 'Home',
  components:{
    NavBarRight
  },
  setup() {
    const store = useStore()
    const router = useRouter()
    
    const dataList = [
      {
        name: 'This PC',
        url: require('@/assets/img/icon/win/userDir.png')
      },
      {
        name: 'Recycle Bin',
        url: require('@/assets/img/icon/bin0.png')
      },
      {
        name: 'File Explorer',
        url: require('@/assets/img/icon/explorer.png')
      },
      {
        name: 'Store',
        url: require('@/assets/img/icon/store.png')
      },
      {
        name: 'Edge',
        url: require('@/assets/img/icon/edge.png')
      },
      {
        name: 'Github',
        url: require('@/assets/img/icon/github.png')
      },
      {
        name: 'Ghosie',
        url: require('@/assets/img/icon/ghosie.png')
      },
    ];
    const scale = item => {
      if(item.name.toLowerCase()==='edge'){
        store.dispatch('desktop/changeEdge')
        return
      }
      if(item.name.toLowerCase()==='github'){
        console.log('aaa');
      }
    };
    
    // 浏览器链接
    const tempData = ['http://localhost:808/']   // 浏览器历史记录
    let currentPosition = 0 // 当前在哪一步
    const edgeLink = ref('http://localhost:808/')
    const edgeLinkData = ref('http://localhost:808/')
    const edgeLinkChange=()=>{
      if(edgeLink.value.indexOf('http://')>-1 || edgeLink.value.indexOf('https://')>-1){
        tempData.push(edgeLink.value)
        currentPosition++
        edgeLinkData.value = edgeLink.value
        return
      }
      if(edgeLink.value.indexOf('www.')>-1||edgeLink.value.indexOf('.co')>-1){
        edgeLinkData.value = edgeLink.value = `https://${edgeLink.value}`
        tempData.push(edgeLink.value)
        currentPosition++
        return
      }
      edgeLinkData.value = edgeLink.value = `https://cn.bing.com/search?q=${edgeLink.value}`
      tempData.push(edgeLink.value)
      currentPosition++
    }
    // 浏览器显示/隐藏
    const edge = computed(()=>store.state.desktop.edge)
    // 关闭窗口
    const closeWindow = val =>{
      if(val === 'edge'){
        store.dispatch('desktop/changeEdge')
      }
    }
    // 前进 后退 刷新
    const urlLeft = val =>{
      if(val==='back'){
        if(currentPosition-1>=0){
          currentPosition--
          edgeLink.value=tempData[currentPosition]
        }
        router.go(-1)
        return
      }
      if(val==='go'){
        if(currentPosition+1<=tempData.length){
          currentPosition++
          edgeLink.value=tempData[currentPosition]
        }
        router.go(1)
        return
      }
      router.go(0)
    }
    // 获取DOM节点
    const appWindow = ref(null)
   
    return {
      dataList,
      scale,
      edgeLink,
      edgeLinkData,
      edgeLinkChange,
      edge,
      closeWindow,
      urlLeft,
      appWindow
    };
  },
};
</script>
<style lang='scss' scoped>
.home {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  align-content: flex-start;
  text-align: left;
  width: 100%;
  box-sizing: border-box;
  padding: 20px 5px;
  height: 100vh;
  .itemTag {
    width: 80px;
    height: 90px;
    text-align: center;
    box-sizing: border-box;
    padding: 15px 0 0;
    margin: 0 10px 20px 0;
    transition: background 1s;
    img {
      width: 35px;
      height: 35px;
      // transform: scale(1);
      // transition: all ease-in 0.2s;
    }
    // img.scale {
    //   transform: scale(0.5);
    // }
    p {
      color: #fafafa;
      text-shadow: 0 0 4px rgb(0 0 0 / 60%);
      font-size: 0.5em;
      width: 100%;
      box-sizing: border-box;
      padding: 0 12px;
      display: -webkit-box;
      word-wrap: break-word;
      -webkit-line-clamp: 2; // 设置显示行数
      text-overflow: ellipsis;
      -webkit-box-orient: vertical;
      overflow: hidden;
    }
  }
  .itemTag:hover {
    background: rgba(255, 255, 255, 0.5);
  }
}
.appWindow{
  position: absolute;
  width: 100%;
  height: 100%;
  overflow: hidden;
  top: 0;
  left: 0;
  display: grid;
  grid-template-rows: 0.35fr 0.35fr 9.3fr;
  .titleBar{
    min-height: 32px;
    background-color: #E7EAEC;
  }
  .draggBox{
    display: flex;
    justify-content: flex-start;
    align-items: center;
    background-color: #F7FAFC;
    .urlLeft{
      padding-left: 20px;
      i{
        margin-right: 24px;
        padding: 3px;
        transform: scale(1.3);
        transition: background .5s;
        border-radius: 3px;
        &:hover{
          background: #DAB591;
        }
      }
    }
  }
  .box2{
    background-color: #fff;
    iframe{
      width: 100%;
      height: 100%;
    }
  }
}
::v-deep .el-input{
  width: 70%;
  .el-input__inner{
    height: 32px;
  }
}
.titleBarLeft{
  height: 100%;
  padding-left: 30px;
  background: url('~@/assets/img/icon/edge.png') no-repeat 6px center/16px;
  text-align: left;
  div{
    display: flex;
    justify-content: space-between;
    width: 192px;
    height: 100%;
    align-items: center;
    background-color: #F7FAFC;
    border-radius: 4px 4px 0 0;
    padding: 0 8px;
    overflow: hidden;
    span,i{
      font-size: 0.72em;
    }
  }
}
.hidden{
  display: none;
}
</style>